package com.chap18;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * <code>SyncQModeDemo</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-05-17 14:01
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
@Slf4j
public class SyncQModeDemo1 {
    final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        SyncQModeDemo1 syncQModeDemo = new SyncQModeDemo1();
        syncQModeDemo.startThreadA();
        Thread.sleep(100);
        syncQModeDemo.startThreadB();
        Thread.sleep(100);
        syncQModeDemo.startThreadC();
    }

    public void startThreadA(){
        new Thread(()->{
            synchronized (lock){
                log.debug("A get lock");
                try {
                    lock.wait(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("A release lock");
            }
        },"thread-A").start();
    }

    public void startThreadB(){
        new Thread(()->{
            synchronized (lock){
                log.debug("B get lock");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("B release lock");
            }
        },"thread-A").start();
    }

    public void startThreadC(){
        new Thread(()->{
            synchronized (lock){
                log.debug("C get lock");
            }
        },"thread-C").start();
    }
}

/*
执行顺序 BAC
*/